package com.example.teatea.controller;

import com.example.teatea.project.Good;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.image.Image;
import javafx.scene.layout.HBox;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.util.Callback;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;

public class OrderListController implements Initializable {
    public Label allPrice;
    public Button returnBt;
    public Button settleBt;
    public ListView<Good> list;
    public static float sum = 0;

    public void toReturn(ActionEvent actionEvent) {
        Stage stage = (Stage) returnBt.getScene().getWindow();
        stage.close();
    }

    public void toSettle(ActionEvent actionEvent) throws IOException {
        Stage stage1 = (Stage) settleBt.getScene().getWindow();
        stage1.close();
        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/com/example/teatea/fxml/SettleAccount.fxml"));
        Scene scene = new Scene(fxmlLoader.load());
        Stage stage = new Stage();
        stage.setScene(scene);
        //设置窗口标题
        stage.setTitle("结账");
        //设置窗体不关闭不能使用其它窗体
        stage.initModality(Modality.APPLICATION_MODAL);
        //设置窗口大小不能改变
        stage.setResizable(false);
        stage.show();
    }

    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {
        sum = 0;
        ObservableList<Good> observableList = FXCollections.observableArrayList();
        for(Good s : SeatController.hashMap.keySet()){
            sum += s.getgPrice() * SeatController.hashMap.get(s);
            observableList.add(s);
        }
        allPrice.setText(String.valueOf(sum));
        list.setItems(observableList);
        //获取每个单元的值
        list.getSelectionModel().selectedItemProperty().addListener(new ChangeListener() {
            @Override
            public void changed(ObservableValue observableValue, Object o, Object t1) {

            }
        });
        //设置为单元工厂
        list.setCellFactory(new Callback<ListView<Good>, ListCell<Good>>() {
            @Override
            public ListCell<Good> call(ListView<Good> listView) {
                return new ListCell<Good>(){
                    @Override
                    protected void updateItem(Good s, boolean b) {
                        super.updateItem(s, b);
                        if(!b){
                            HBox hBox = new HBox();
                            FXMLLoader fxmlLoader = new FXMLLoader();
                            fxmlLoader.setLocation(getClass().getResource("/com/example/teatea/fxml/Single.fxml"));
                            try {
                                hBox.getChildren().add(fxmlLoader.load());
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                            SingleController singleController = fxmlLoader.getController();
                            hBox.setPadding(new Insets(0,0,0,0));
                            singleController.setName(s.getgName());
                            singleController.setPrice(s.getgPrice());
                            singleController.setSum(SeatController.hashMap.get(s));
                            this.setGraphic(hBox);
                        }else{
                            this.setGraphic(null);
                        }
                    }
                };
            }
        });
    }
}
